package com.cn.kehong.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cn.kehong.api.IProductPeiTouService;
import com.cn.kehong.domain.ProductPeiJobSupplier;
import com.cn.kehong.domain.ProductPeiTou;
import com.cn.kehong.domain.SysItem;
import com.cn.kehong.utils.CommonUtil;
import com.cn.kehong.utils.JsonResponse;
import com.cn.kehong.utils.QueryForm;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/peitou")
public class ProductPeiTouController {

    @Autowired
    private IProductPeiTouService productPeiTouService;

    @RequestMapping("/getProductPeiTouList/{frontToken}")
    @ResponseBody
    public JsonResponse<List<ProductPeiTou>> getProductPeiTouList(QueryForm queryForm, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<List<ProductPeiTou> > response = new JsonResponse<List<ProductPeiTou> >();
        List<ProductPeiTou> lists = productPeiTouService.getProductPeiTouList(queryForm);
        response.setResult(lists);
        response.setSuccess(true);
        return  response;
    }

    @RequestMapping("/getProductPeiTouDutyDayList/{frontToken}")
    @ResponseBody
    public JsonResponse<List<ProductPeiTou>> getProductPeiTouDutyDayList(QueryForm queryForm, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<List<ProductPeiTou> > response = new JsonResponse<List<ProductPeiTou> >();
        List<ProductPeiTou> lists = productPeiTouService.getProductPeiTouDutyDayList(queryForm);
        response.setResult(lists);
        response.setSuccess(true);
        return  response;
    }

    @RequestMapping("/getProductUnCompleteOrDutyPeiTous/{frontToken}")
    @ResponseBody
    public JsonResponse<List<ProductPeiTou>> getProductUnCompleteOrDutyPeiTous(QueryForm queryForm, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<List<ProductPeiTou> > response = new JsonResponse<List<ProductPeiTou> >();
        List<ProductPeiTou> lists = productPeiTouService.getProductUnCompleteOrDutyPeiTous(queryForm);
        response.setResult(lists);
        response.setSuccess(true);
        return  response;
    }

    @RequestMapping("/getProductPeiTouDutyDayCountMap/{frontToken}")
    @ResponseBody
    public JsonResponse<Map<String,Integer>> getProductPeiTouDutyDayCountMap(QueryForm queryForm, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<Map<String,Integer> > response = new JsonResponse<Map<String,Integer> >();
        Map<String,Integer> result = productPeiTouService.getProductPeiTouDutyDayCountMap(queryForm);
        response.setResult(result);
        response.setSuccess(true);
        return  response;
    }

    @RequestMapping("/getProductPeiTouCount/{frontToken}")
    @ResponseBody
    public JsonResponse<String> getProductPeiTouCount(@PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<String > response = new JsonResponse<String >();
        String result = productPeiTouService.getProductPeiTouCount();
        response.setResult(result);
        response.setSuccess(true);
        return  response;
    }

    @RequestMapping("/addProductPeiTou/{frontToken}")
    @ResponseBody
    public JsonResponse<String> addProductPeiTou(ProductPeiTou productPeiTou, @PathVariable(value =  "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<String> response = new JsonResponse<>();
        String result = productPeiTouService.addProductPeiTou(productPeiTou);
        return CommonUtil.getResponseData(response,result);
    }

    @RequestMapping("/updateProductPeiTou/{frontToken}")
    @ResponseBody
    public JsonResponse<String> updateProductPeiTou(ProductPeiTou productPeiTou, @PathVariable(value =  "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<String> response = new JsonResponse<>();
        String result = productPeiTouService.updateProductPeiTou(productPeiTou);
        return CommonUtil.getResponseData(response,result);
    }

    @RequestMapping("/deleteProductPeiTou/{frontToken}")
    @ResponseBody
    public JsonResponse<String> deleteProductPeiTou(String id, @PathVariable(value =  "frontToken",required = true) String frontToken) throws Exception{
        JsonResponse<String> response = new JsonResponse<>();
        String result = productPeiTouService.deleteProductPeiTou(id);
        return CommonUtil.getResponseData(response,result);
    }

    //获取待配料和正在配料的个数
    @RequestMapping("/api/getProductPeiTouDutyDayCountMap")
    @ResponseBody
    public JsonResponse<Map<String,Integer>> getAPIProductPeiTouDutyDayCountMap(QueryForm queryForm) throws Exception {
        JsonResponse<Map<String,Integer> > response = new JsonResponse<Map<String,Integer> >();
        Map<String,Integer> result = productPeiTouService.getProductPeiTouDutyDayCountMap(queryForm);
        response.setResult(result);
        response.setSuccess(true);
        return  response;
    }


    @RequestMapping("/api/getProductPeiTouDutyDayList")
    @ResponseBody
    public JsonResponse<List<ProductPeiTou>> getAPIProductPeiTouDutyDayList(QueryForm queryForm) throws Exception {
        JsonResponse<List<ProductPeiTou> > response = new JsonResponse<List<ProductPeiTou> >();
        List<ProductPeiTou> lists = productPeiTouService.getProductPeiTouDutyDayList(queryForm);
        response.setResult(lists);
        response.setSuccess(true);
        return  response;
    }
    @RequestMapping("/api/getCompleteProductPeiTouDutyDayList")
    @ResponseBody
    public JsonResponse<List<ProductPeiTou>> getAPICompleteProductPeiTouDutyDayList(QueryForm queryForm) throws Exception {
        JsonResponse<List<ProductPeiTou> > response = new JsonResponse<List<ProductPeiTou> >();
        List<ProductPeiTou> lists = productPeiTouService.getProductPeiTouList(queryForm);
        response.setResult(lists);
        response.setSuccess(true);
        return  response;
    }

    //扫码
    @RequestMapping("/api/getScanQRCode")
    @ResponseBody
    public JsonResponse<Map<String,Object>> getScanQRCode(String qrCode) throws Exception {
        JsonResponse<Map<String,Object> > response = new JsonResponse<Map<String,Object> >();
        Map<String,Object> result = productPeiTouService.getScanQRCode(qrCode);
        response.setResult(result);
        String errorMsg = (String) result.get("errorMsg");
        response.setSuccess(StringUtils.isEmpty(errorMsg)?true:false);
        return  response;
    }

    //获取物料
    @RequestMapping("/api/getProductPeiJobSuppliersByPeiId")
    @ResponseBody
    public JsonResponse<List<ProductPeiJobSupplier>> getAPICompleteProductPeiTouDutyDayList(String peiId) throws Exception {
        JsonResponse<List<ProductPeiJobSupplier> > response = new JsonResponse<List<ProductPeiJobSupplier> >();
        List<ProductPeiJobSupplier> lists = productPeiTouService.getProductPeiJobSuppliersByPeiId(peiId);
        response.setResult(lists);
        response.setSuccess(true);
        return  response;
    }

    //保存配料
    @RequestMapping("/api/addAPIProductPeiJobs")
    @ResponseBody
    public JsonResponse<String> addAPIProductPeiJobs(@RequestBody ProductPeiTou productPeiTou) throws Exception {
        JsonResponse<String > response = new JsonResponse<String >();
        System.out.println("=============addAPIProductPeiJobs params:==============");
        System.out.println(productPeiTou);
        String result = productPeiTouService.addAPIProductPeiJobs(productPeiTou);
        return CommonUtil.getResponseData(response,result);
    }

    //测试保存配料
    @RequestMapping("/api/addTestAPIProductPeiJobs")
    @ResponseBody
    public JsonResponse<String> addTestAPIProductPeiJobs(String peiStatus,String id) throws Exception {
        JsonResponse<String > response = new JsonResponse<String >();
        ProductPeiTou productPeiTou = new ProductPeiTou();
        productPeiTou.setId(id);
        productPeiTou.setPeiStatus(peiStatus);
        productPeiTou.setPeiUserName("张三");
        productPeiTou.setPeiStartTime("2024-09-23 12:03:05");

        List<ProductPeiJobSupplier> productPeiJobSuppliers = Lists.newArrayList();
        ProductPeiJobSupplier supplier1 = new ProductPeiJobSupplier();
        supplier1.setMaterialName("磷酸铁（纳源）");
        supplier1.setMaterialType("磷酸铁");
        supplier1.setMaterialCode("1234567890");
        supplier1.setMaterialId("ba10d8d2item-834aitem-424bitem-87ccitem-ef4e73204418");
        supplier1.setPeiType("1");
        supplier1.setProductTime("240922");
        supplier1.setProductCode("170909");
        supplier1.setSupplierName("南京科技");
        supplier1.setSupplierCode("610441");
        supplier1.setSupplierId("a510b550sysSupplier_33adsysSupplier_4a29sysSupplier_8b7asysSupplier_1bd27b315070");
        supplier1.setPeiWeight("800");
        supplier1.setPeiNum("3");
        productPeiJobSuppliers.add(supplier1);

        ProductPeiJobSupplier supplier2 = new ProductPeiJobSupplier();
        supplier2.setMaterialName("磷酸铁（纳源材料）");
        supplier2.setMaterialType("磷酸铁");
        supplier2.setMaterialCode("1234567892");
        supplier2.setMaterialId("c2696320item-160fitem-4c05item-b376item-7e144de591b3");
        supplier2.setPeiType("2");
        supplier2.setProductTime("240922");
        supplier2.setProductCode("170909");
        supplier2.setSupplierName("南京科技");
        supplier2.setSupplierCode("610441");
        supplier2.setSupplierId("a510b550sysSupplier_33adsysSupplier_4a29sysSupplier_8b7asysSupplier_1bd27b315070");
        supplier2.setPeiWeight("800");
        supplier2.setPeiNum("3");
        productPeiJobSuppliers.add(supplier2);
        productPeiTou.setProductPeiJobSuppliers(productPeiJobSuppliers);
        String result = productPeiTouService.addAPIProductPeiJobs(productPeiTou);
        return CommonUtil.getResponseData(response,result);
    }

    //app配料数量
    @RequestMapping("/getProductPeiTouDutyDayCount")
    @ResponseBody
    public JsonResponse<Map<String,Object>> getProductPeiTouDutyDayCount(@RequestBody QueryForm queryForm) throws Exception {
        JsonResponse<Map<String,Object> > response = new JsonResponse<Map<String,Object> >();
        Map<String,Object> result = productPeiTouService.getProductPeiTouDutyDayCount(queryForm);
        response.setResult(result);
        response.setSuccess(true);
        return  response;
    }

    //投入产出-单班投入产出查询
    @RequestMapping("/getProduceByDay/{frontToken}")
    @ResponseBody
    public JsonResponse<List<ProductPeiTou>> getProduceByDay(QueryForm queryForm, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<List<ProductPeiTou> > response = new JsonResponse<List<ProductPeiTou> >();
        List<ProductPeiTou> lists = productPeiTouService.getProduceByDay(queryForm);
        response.setResult(lists);
        response.setSuccess(true);
        return  response;
    }
    //投入产出-单班投入产出查询下载
    @ResponseBody
    @RequestMapping("/exportProduceByDay")
    public void exportProduceByDay(String queryStr, HttpServletResponse response) throws Exception {
        QueryForm queryForm = new QueryForm();
        if(StringUtils.isNotEmpty(queryStr)){
            queryStr = URLDecoder.decode(queryStr,"utf-8");
            queryStr = new String(queryStr.getBytes("ISO-8859-1"),"utf-8");
            JSONObject jsonObject = JSON.parseObject(queryStr);
            queryForm.setStartTime(jsonObject.getString("startTime"));
            queryForm.setEndTime(jsonObject.getString("endTime"));
            queryForm.setFactoryName(jsonObject.getString("factoryName"));
            queryForm.setBanCi(jsonObject.getString("banCi"));
            queryForm.setGradeValue(jsonObject.getString("gradeValue"));
        }
        productPeiTouService.exportProduceByDay(queryForm,response);
    }


    //投入产出-单日投入产出查询
    @RequestMapping("/getProduceByBanCi/{frontToken}")
    @ResponseBody
    public JsonResponse<List<ProductPeiTou>> getProduceByBanCi(QueryForm queryForm, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<List<ProductPeiTou> > response = new JsonResponse<List<ProductPeiTou> >();
        List<ProductPeiTou> lists = productPeiTouService.getProduceByBanCi(queryForm);
        response.setResult(lists);
        response.setSuccess(true);
        return  response;
    }


    //投入产出-单班投入产出查询下载
    @ResponseBody
    @RequestMapping("/exportProduceByBanCi")
    public void exportProduceByBanCi(String queryStr, HttpServletResponse response) throws Exception {
        QueryForm queryForm = new QueryForm();
        if(StringUtils.isNotEmpty(queryStr)){
            queryStr = URLDecoder.decode(queryStr,"utf-8");
            queryStr = new String(queryStr.getBytes("ISO-8859-1"),"utf-8");
            JSONObject jsonObject = JSON.parseObject(queryStr);
            queryForm.setStartTime(jsonObject.getString("startTime"));
            queryForm.setEndTime(jsonObject.getString("endTime"));
            queryForm.setFactoryName(jsonObject.getString("factoryName"));

        }
        productPeiTouService.exportProduceByBanCi(queryForm,response);
    }


    //主页

    @RequestMapping("/getProductPeiTouToday/{frontToken}")
    @ResponseBody
    public JsonResponse<List<JSONObject>> getProductPeiTouToday( @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<List<JSONObject> > response = new JsonResponse<List<JSONObject> >();
        List<JSONObject> lists = productPeiTouService.getProductPeiTouToday();
        response.setResult(lists);
        response.setSuccess(true);
        return  response;
    }

    //通过制程批号查询规格
    @RequestMapping("/getGradeByCode")
    @ResponseBody
    public JsonResponse<List<ProductPeiTou>> getGradeByCode(@RequestBody ProductPeiTou productPeiTou) throws Exception {
        JsonResponse<List<ProductPeiTou> > response = new JsonResponse<List<ProductPeiTou> >();
        List<ProductPeiTou> lists = productPeiTouService.getGradeByCode(productPeiTou);
        response.setResult(lists);
        response.setSuccess(true);
        return  response;
    }


    @RequestMapping("/getProductPeiTouList1/{frontToken}")
    @ResponseBody
    public JsonResponse<List<ProductPeiTou>> getProductPeiTouList1(QueryForm queryForm, @PathVariable(value = "frontToken",required = true) String frontToken) throws Exception {
        JsonResponse<List<ProductPeiTou> > response = new JsonResponse<List<ProductPeiTou> >();
        List<ProductPeiTou> lists = productPeiTouService.getProductPeiTouList1(queryForm);
        response.setResult(lists);
        response.setSuccess(true);
        return  response;
    }
}
